//
//  MemberCampaignItemView.m
//  HomePage
//
//  Created by xiong qi on 13-5-5.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "MemberCampaignItemView.h"

@implementation MemberCampaignItemView
@synthesize dataSource,reuseIdentifier,parentDelegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        [self setBackgroundColor:[UIColor whiteColor]];
        
        imageview = [[UIImageView alloc] init];
        [self addSubview:imageview];
        [imageview release];
        
        [self addTarget:self action:@selector(onTap:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)loadcelldata:(NSObject *)data
{
    self.dataSource = (NSDictionary *)data;

    imageview.image = [MemberCampaignItemView GetImage:[dataSource objectForKey:Key_Data]];
}

-(void)ResetFrame
{
    float height = [[dataSource objectForKey:Key_Height] floatValue];
    imageview.frame = CGRectMake((self.frame.size.width-WaterFallItemImageWidth)/2, (self.frame.size.height-height)/2, WaterFallItemImageWidth, height);
}

-(void)clearImage
{
    
}

-(void)onTap:(id)sender
{
    if ([parentDelegate respondsToSelector:@selector(CellBeTouch:)]) {
        [parentDelegate performSelector:@selector(CellBeTouch:) withObject:self];
    }
}

+(UIImage *)GetImage:(NSString *)data
{
    if (data == nil) {
        return nil;
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * localpath= [NSString stringWithFormat:@"%@/paddata",[paths objectAtIndex:0]];
    NSString * path = [NSString stringWithFormat:@"%@/%@",localpath,data];
    UIImage * image = [[UIImage alloc] initWithContentsOfFile:path];

    return [image autorelease];
}

+(CGFloat) caculateCellHeight:(NSMutableDictionary*)Info
{
    
    UIImage * image = [MemberCampaignItemView GetImage:[Info objectForKey:Key_Data]];
    CGFloat height = 0;
    if (image) {
        height = (image.size.height * WaterFallItemImageWidth)/image.size.width;
        
    }
    
   [Info setObject:[NSNumber numberWithFloat:height] forKey:Key_Height];
    
    return height;
}

-(void)dealloc
{
    self.dataSource = nil;
    self.reuseIdentifier = nil;
    self.parentDelegate = nil;
    
    [super dealloc];
}

@end
